let Article = require('../models/article')

//添加文章
const add = async ctx => {
    let article = ctx.request.body
    await Article.create(article).then(rel => {
        if (rel) {
            ctx.body = {
                code: 200,
                data: '发布文章成功'
            }
        } else {
            ctx.body = {
                code: 300,
                data: '发布文章失败'
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            data: '发布文章异常'
        }
    })

}
/**
 * 查询所有文章（分页）
 */
const findAll = async ctx => {
    let { page, author, query='' } = ctx.query //page页码

    //判断页码
    if(!page || isNaN(Number(page))) {
        page = 1
    }else {
        page = Number(page)
    }
    //每页条数
    let pageSize = 10
    //计算总页数
    let count = 0 //文章总条数
    await Article.find({author}).count().then(rel => {
        count = rel
    })
    let totalPage = 0 //文章总页数
    if(count > 0) {
        totalPage = Math.ceil(count / pageSize)
    }

    //判断当前页码范围
    if(totalPage > 0 && page > totalPage) {
        page = totalPage
    }else if(page < 1) {
        page = 1
    }
    //计算起始位置
    let start = (page - 1) * pageSize
    await Article.find({author, "title": {$regex: query}}).skip(start).limit(pageSize).then(rel=> {
        console.log(rel);
        if(rel && rel.length > 0) {
            ctx.body = {
                code: 200,
                msg: "文章查询成功",
                reult: rel,
                page,
                pageSize,
                count
            }
        }else {
            ctx.body = {
                code: 300,
                msg: "文章查询失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "文章查询异常"
        }
        console.log(err);
    })
}
/**
 * 查询单个文章（根据ID查询）
 */
const find = async ctx => {
    let { id } = ctx.query
    let isRead = false
    await Article.findOne({id}).then(rel => {
        if(rel) {
            isRead = true
            ctx.body = {
                code: 200,
                msg: "文章查询成功",
                reult: rel
            }
        }else {
            ctx.body = {
                code: 300,
                msg: "文章查询失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "文章查询异常"
        }
        console.log(err);
    })
    if(isRead) {
        await Article.updateOne({id},{$inc: {read: 1}})
    }

}
/**
 * 修改文章
 */
const update = async ctx => {
    let article = ctx.request.body
    await Article.updateOne(
        {id: article.id},
        {
            title: article.title,
            stemfrom: article.stemfrom,
            content: article.content
        }).then(rel => {
            if(rel.modifiedCount > 0) {
                ctx.body = {
                    code: 200,
                    msg: "文章更新成功"
                }
            }else {
                ctx.body = {
                    code: 300,
                    msg: "文章更新失败"
                }
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: "文章更新异常",
            }
        })
}
/**
 * 删除文章
 */
const del = async ctx => {
    let { id } = ctx.request.body
    await Article.findOneAndDelete({id}).then(rel => {
        console.log('111',rel);
        if(rel) {
            ctx.body = {
                code: 200,
                msg: "文章删除成功"
            }
        }else {
            ctx.body = {
                code: 300,
                msg: "文章s删除失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "文章删除异常",
        }
        console.log(err);
    })

}

module.exports = {
    add,
    find,
    findAll,
    update,
    del
}